﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using Ccit.Utils;
using Ccit.OA.Utils;
using Ccit.Global;
using System.Collections;

namespace Ccit.OA.NewsCenter
{
    public class ColumnSQL : IColumn
    {
        #region 信息中心-栏目管理
        /// <summary>
        /// 显示树
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns></returns>
        public List<MsColumn> GetBindTree(string type)
        {
            string sTreeData = "";
            if (string.IsNullOrEmpty(type))
            {
                sTreeData = "SELECT NEWS_A2_10,NEWS_A2_20,NEWS_A2_30 FROM NEWS_A2 WHERE NEWS_A2_120='0' AND NEWS_A2_140='0' ";
            }
            else
            {
                sTreeData = "SELECT NEWS_A2_10,NEWS_A2_20,NEWS_A2_30 FROM NEWS_A2 WHERE NEWS_A2_120='" + type + "' AND NEWS_A2_140='0'";
            }

            sTreeData += "  ORDER BY NEWS_A2_30, NEWS_A2_130 ";

            List<MsColumn> mclist = new List<MsColumn>();
            using(IDataReader read=DataAccessor.ExecuteReader(sTreeData))
            {
                while(read.Read()){
                    MsColumn mc = new MsColumn();
                    mc.columnId = read["NEWS_A2_10"].ToString();
                    mc.columnName = read["NEWS_A2_20"].ToString();
                    mc.columnLevelCode = read["NEWS_A2_30"].ToString();
                    mclist.Add(mc);
                }
            }
            return mclist;
        }
        /// <summary>
        /// 获取单个对象
        /// </summary>
        /// <param name="columnId">主键</param>
        /// <returns></returns>
        public MsColumn GetMsColumnObj(string columnId)
        {
            
            if (string.IsNullOrEmpty(columnId)) return null;
            MsColumn mc = new MsColumn();
            string sMsColumnObj = "SELECT NEWS_A2_10,NEWS_A2_20,NEWS_A2_30,NEWS_A2_40,NEWS_A2_50,NEWS_A2_60,NEWS_A2_70,NEWS_A2_80,NEWS_A2_90,NEWS_A2_100,NEWS_A2_110,NEWS_A2_130 FROM NEWS_A2 WHERE NEWS_A2_10='" + columnId + "'";
          
            using (IDataReader read = DataAccessor.ExecuteReader(sMsColumnObj))
            {
                while (read.Read())
                {
                    mc.columnId = read["NEWS_A2_10"].ToString();
                    mc.columnName = read["NEWS_A2_20"].ToString();
                    mc.columnLevelCode = read["NEWS_A2_30"].ToString();
                    mc.pubType = read["NEWS_A2_40"].ToString();
                    mc.pubPurviewId = read["NEWS_A2_50"].ToString();
                    mc.pubPurviewType = read["NEWS_A2_60"].ToString();
                    mc.managePurview = read["NEWS_A2_70"].ToString();
                    mc.managePurviewType = read["NEWS_A2_80"].ToString();
                    mc.browsePurview = read["NEWS_A2_90"].ToString();
                    mc.browsePurviewType = read["NEWS_A2_100"].ToString();
                    mc.columnDesc = read["NEWS_A2_110"].ToString();
                    mc.orderNo = read["NEWS_A2_130"].ToString();
                }
            }
            return mc;
        }
        /// <summary>
        /// 获取所有子节点
        /// </summary>
        /// <param name="columnId">主键</param>
        /// <returns></returns>
        public List<MsColumn> GetSubColumnTypes(string columnId)
        {
            if (string.IsNullOrEmpty(columnId)) return null;
           // string sType = (Type != "") ? Type : "0";  
            string SubCode = GetMsColumnObj(columnId).columnLevelCode;
            int layCodeLen = SubCode.Length;
            int nextLayCodeLen = layCodeLen + 4;
            string cmdText = "SELECT NEWS_A2_10 ,NEWS_A2_20 ,NEWS_A2_30  FROM NEWS_A2 " +
                             "WHERE NEWS_A2_30 LIKE '" + SubCode + "%' AND LEN(NEWS_A2_30) =" + nextLayCodeLen +
                             " AND NEWS_A2_140='0' ";

            cmdText += "  ORDER BY NEWS_A2_30, NEWS_A2_130 ";

            List<MsColumn> list = new List<MsColumn>();
            using (IDataReader read = DataAccessor.ExecuteReader(cmdText))
            {

                while (read.Read())
                {
                    MsColumn m = new MsColumn();
                    m.columnId = read["NEWS_A2_10"].ToString();
                    m.columnName = read["NEWS_A2_20"].ToString();
                    m.columnLevelCode = read["NEWS_A2_30"].ToString();
                    list.Add(m);
                }
            }
            return list;
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="mc"></param>
        /// <returns></returns>
        public List<string> InsertColumn(MsColumn mc)
        {
            List<string> slist = new List<string>();
            string sExecute = "";
            string hasChild = "-1";
            string strMaxId = mc.columnId;
            string sType = (mc.infoType != "") ? mc.infoType : "0";   //信息类型
            if (string.IsNullOrEmpty(strMaxId))//增加
            {
                hasChild = "0";
                string[] pubMsg = SessionExt.GetSysFieldSQL("NEWS_A2");
                strMaxId = DataInterface.GetMaxId("NEWS_A2", "NEWS_A2_10").ToString();
                MsColumn m = new MsColumn();
                m.columnId = strMaxId;
                m.columnLevelCode = getNewCode(mc.columnLevelCode);
                m.columnName = mc.columnName;
                m.pubType = mc.pubType;
                m.pubPurviewType = mc.pubPurviewType;
                m.managePurviewType = mc.managePurviewType;
                m.browsePurviewType = mc.browsePurviewType;
                m.orderNo = mc.orderNo;
                m.columnDesc = mc.columnDesc;
                
                sExecute = "INSERT INTO NEWS_A2 (NEWS_A2_10,NEWS_A2_20,NEWS_A2_30,NEWS_A2_40,NEWS_A2_50,NEWS_A2_60,NEWS_A2_70,NEWS_A2_80,NEWS_A2_90,NEWS_A2_100,NEWS_A2_110,NEWS_A2_120,NEWS_A2_130,NEWS_A2_140,"+pubMsg[0]+")";
                sExecute += "VALUES ('" + strMaxId + "','" + mc.columnName + "','" + getNewCode(mc.columnLevelCode) + "','" + mc.pubType + "','"+mc.pubPurviewId+"','" + mc.pubPurviewType + "','"+mc.managePurview+"','" + mc.managePurviewType + "','"+mc.browsePurview+"','" + mc.browsePurviewType + "','" + mc.columnDesc + "','"+sType+"','" + mc.orderNo + "',0,"+pubMsg[1]+")";               
            }
            else//修改
            {             
                sExecute = "UPDATE NEWS_A2 SET NEWS_A2_20='" + mc.columnName + "' ";
                sExecute += ",NEWS_A2_40='" + mc.pubType + "',NEWS_A2_50='" + mc.pubPurviewId + "',NEWS_A2_60='" + mc.pubPurviewType + "',NEWS_A2_70='" + mc.managePurview + "',NEWS_A2_80='" + mc.managePurviewType + "'";
                sExecute += ",NEWS_A2_90='" + mc.browsePurview + "',NEWS_A2_100='" + mc.browsePurviewType + "',NEWS_A2_110='" + mc.columnDesc + "',NEWS_A2_130='" + mc.orderNo + "',NEWS_A2_140=0 ";
                MsColumn fatherObj = this.GetFatherObj(mc.columnId);
                if(fatherObj.columnId!=mc.columnLevelCode){//更改父节点时修改层级编号
                    sExecute += ",NEWS_A2_30='" + getNewCode(mc.columnLevelCode) + "'";
                }
                sExecute += " WHERE NEWS_A2_10='" + mc.columnId + "'";
                MsColumn mOld = this.GetMsColumnObj(mc.columnId);//获取修改前的栏目对象
                if (isHasChildType(mOld.columnLevelCode))
                {
                    string newCode = mOld.columnLevelCode.Replace(mOld.columnLevelCode, getNewCode(mc.columnLevelCode));
                    List<MsColumn> childList = getChildList(mOld.columnLevelCode);
                    foreach(MsColumn mChild in childList){
                        string childNewCode = mChild.columnLevelCode.Replace(mOld.columnLevelCode,newCode);
                        string MotifyChild = "UPDATE NEWS_A2 SET NEWS_A2_30='" + childNewCode + "' WHERE NEWS_A2_10 = '" +mChild.columnId + "'";
                        int iexe = DataAccessor.ExecuteNonQuery(MotifyChild);
                    }
                }

                MsColumn msCurCol = GetMsColumnObj(strMaxId);
                if (isHasChildType(msCurCol.columnLevelCode))
                {
                    hasChild = "1";
                }
                else
                    hasChild = "0";
            }

            if (0 < DataAccessor.ExecuteNonQuery(sExecute))
            {
                slist.Add("1");
                slist.Add("保存成功");
                slist.Add(strMaxId);
                if (mc.columnLevelCode == "(无上级类别)")
                    slist.Add("0");
                else
                    slist.Add(mc.columnLevelCode);

                slist.Add(hasChild);
            }
            else
            {
                slist.Add("-1");
                slist.Add("服务器忙,保存失败，请稍候再试");
                slist.Add("error");
                slist.Add(mc.columnLevelCode);
                slist.Add("-1");
            }
            
            return slist;
        }
        private List<MsColumn> getChildList(string columnCode)
        {
            string str = "select NEWS_A2_10,NEWS_A2_20,NEWS_A2_30 from NEWS_A2 where NEWS_A2_30 like '"+columnCode+"%'";
            List<MsColumn> list = new List<MsColumn>();
            using (IDataReader read = DataAccessor.ExecuteReader(str))
            {
                while (read.Read())
                {
                    MsColumn m = new MsColumn();
                    m.columnId = read["NEWS_A2_10"].ToString();
                    m.columnName = read["NEWS_A2_20"].ToString();
                    m.columnLevelCode = read["NEWS_A2_30"].ToString();
                    list.Add(m);
                }
            }
            return list;
        }
        //获取新层级编号
        private string getNewCode(string fColumnId)
        {
            string sMaxCode = "";
            string maxColumnCode = string.Empty;
            string strMaxCode = string.Empty;
            if (fColumnId == "(无上级类别)")
            {
                strMaxCode = "select (max(NEWS_A2_30)+1) from NEWS_A2  where len(NEWS_A2_30)=4 ";
                maxColumnCode = DataAccessor.ExecuteScalar(strMaxCode).ToString();
                if (string.IsNullOrEmpty(maxColumnCode))
                {
                    maxColumnCode = "0001";
                }
                if (maxColumnCode.Length < 4)
                {
                    int num = 4 - maxColumnCode.Length;
                    for (int i = 0; i < num; i++)
                    {
                        maxColumnCode= "0" + maxColumnCode;
                    }
                }
                sMaxCode = maxColumnCode;
                
            }
            else
            {
                string fcolumnCode = this.GetMsColumnObj(fColumnId).columnLevelCode;
                string ChildCodeLength = "select len(NEWS_A2_30) from NEWS_A2 where NEWS_A2_30='" + fcolumnCode + "'";
                int leng = int.Parse(DataAccessor.ExecuteScalar(ChildCodeLength).ToString());
                strMaxCode = "select max(NEWS_A2_30) from NEWS_A2 where len(NEWS_A2_30)='" + leng + "'+4";
                strMaxCode += " and NEWS_A2_30 like '" + fcolumnCode + "%'";
                maxColumnCode = DataAccessor.ExecuteScalar(strMaxCode).ToString();
                if (string.IsNullOrEmpty(maxColumnCode))
                {
                    sMaxCode = fcolumnCode + "0001";
                }
                else
                {
                    long   iMaxCode =Convert.ToInt64(maxColumnCode);
                    sMaxCode = (++iMaxCode).ToString();
                    if (sMaxCode.Length < leng + 4)
                    {
                        int num = leng + 4 - sMaxCode.Length;
                        for (int i = 0; i < num; i++)
                        {
                            sMaxCode = "0" + sMaxCode;
                        }
                    }
                }
            }
            return sMaxCode;
        }
        /// <summary>
        /// 获取下拉列表
        /// </summary>
        /// <param name="type">信息类型</param>
        /// <returns></returns>
        public List<MsColumn> GetDropDownList(string type,string columnId)
        {
            string sType = (type != "") ? type : "0";
            int iBmLen = 0;
            string sNodeMc = "";
            
            string childCodes = this.GetChildLevelCode(columnId);

            List<MsColumn> mclist = new List<MsColumn>();

            string cmdText = "SELECT NEWS_A2_10 ,NEWS_A2_20,NEWS_A2_30 FROM NEWS_A2 WHERE 1=1 AND NEWS_A2_120 = '" + sType + "' AND NEWS_A2_140 = '0'";
            if (!string.IsNullOrEmpty(childCodes))
                cmdText += " AND NEWS_A2_30 NOT IN ("+childCodes+")";

            cmdText += " ORDER BY NEWS_A2_30";
            using (IDataReader read = DataAccessor.ExecuteReader(cmdText))
            {
                while (read.Read())
                {
                    MsColumn mc = new MsColumn();
                    mc.columnId = read["NEWS_A2_10"].ToString();
                    mc.columnName = read["NEWS_A2_20"].ToString();
                    mc.columnLevelCode = read["NEWS_A2_30"].ToString();
                    mclist.Add(mc);
                }
            }
            
            List<MsColumn> rlist = new List<MsColumn>();
            foreach (MsColumn mc in mclist)
            {
                iBmLen = mc.columnLevelCode.ToString().Length / 4;
                if (iBmLen != 1)
                {
                    sNodeMc = "";
                    for (int j = 0; j < iBmLen - 1; j++)
                    {
                        sNodeMc += "\u3000";
                    }
                    mc.columnName = sNodeMc + mc.columnName.ToString();
                } rlist.Add(mc);
            }
            return rlist;
        }
        /// <summary>
        /// 判断是否有子类
        /// </summary>
        /// <param name="astLayCode"></param>
        /// <returns></returns>
        public bool isHasChildType(string columnCode)
        {
            bool bHas = false;
            if (string.IsNullOrEmpty(columnCode)) return bHas;
            int layCodeLen = columnCode.Length;
            string cmdText = "SELECT COUNT(NEWS_A2_30) AS CHILDCOUNT FROM NEWS_A2 WHERE NEWS_A2_30 LIKE '" + columnCode + "%' " +
                             "AND LEN(NEWS_A2_30)>" + layCodeLen + " And NEWS_A2_140='0'";
            string childCount = "";
            using (IDataReader read = DataAccessor.ExecuteReader(cmdText))
            {
                while (read.Read())
                {
                    childCount = read["CHILDCOUNT"].ToString();
                    if (!string.IsNullOrEmpty(childCount) && childCount != "0")
                        bHas = true;
                }
            }
            return bHas;
        }
        /// <summary>
        /// 获取父节点对象
        /// </summary>
        /// <param name="columnId">主键</param>
        /// <returns></returns>
        public MsColumn GetFatherObj(string columnId)
        {
            if (string.IsNullOrEmpty(columnId)) return null ;
            MsColumn mc = null;
            string childCode = this.GetMsColumnObj(columnId).columnLevelCode;
            string fatherCode = "";
            if (childCode.Length > 4)
            {
                fatherCode = childCode.Substring(0, childCode.Length - 4);
            }
            else
            {
                mc = new MsColumn();
                mc.columnId ="0";
                return mc;
            }
            string sCode = "SELECT NEWS_A2_10,NEWS_A2_20,NEWS_A2_30 FROM NEWS_A2 WHERE NEWS_A2_30='"+fatherCode+"'";
            using (IDataReader read = DataAccessor.ExecuteReader(sCode))
            {
                while(read.Read()){
                    mc= new MsColumn();
                    mc.columnId = read["NEWS_A2_10"].ToString();
                    mc.columnName = read["NEWS_A2_20"].ToString();
                    mc.columnLevelCode = read["NEWS_A2_30"].ToString();
                }
            }
            return mc;
        }
        /// <summary>
        /// 删除节点
        /// </summary>
        /// <param name="columnId"></param>
        /// <returns></returns>
        public List<string> RemoveNode(string columnId)
        {
            List<string> slist = new List<string>();
            if(string.IsNullOrEmpty(columnId)){
                slist.Add("-999");
                slist.Add("缺少必要的参数");
            }
            string columnCode = this.GetMsColumnObj(columnId).columnLevelCode;
            bool hasChild = this.isHasChildType(columnCode);
            if(hasChild){
                slist.Add("-998");
                slist.Add("提示:当前资产类别下包含子类,不能删除!");
                return slist;
            }
            string cmdText = " UPDATE NEWS_A2 SET NEWS_A2_140='1' WHERE NEWS_A2_10='"+columnId+"'";
            string sSql = "DELETE FROM NEWS_A3 WHERE NEWS_A3_20 = '"+columnId+"'";

            ArrayList list = new ArrayList();
            list.Add(cmdText);
            list.Add(sSql);

            int iResult = DataAccessor.ExecuteNonQuery(list);
            if (iResult > 0)
            {
                slist.Add("1");
                slist.Add("删除成功!");
            }
            else
            {
                slist.Add("-1");
                slist.Add("服务器忙,请稍候再试!");
            }
            return slist;
        }

        /// <summary>
        /// 获取传入节点的上级节点结构Id
        /// </summary>
        /// <param name="dtNodeId"></param>
        /// <returns></returns>
        public string GetNodeLevelStruct(string dtNodeId)
        {
            string sReturnStruct = string.Empty;
            if (string.IsNullOrEmpty(dtNodeId)) return sReturnStruct;

            MsColumn msCol = GetMsColumnObj(dtNodeId);
            string curNodeCode = (null != msCol && !string.IsNullOrEmpty(msCol.columnLevelCode)) ?
                msCol.columnLevelCode : "";

            int iCodeLen = curNodeCode.Length;
            int iLev = iCodeLen / 4;
            StringBuilder sbClause = new StringBuilder(" 1=1 ");

            string sClause = "";
            string whereClause = "";
            for (int i = iLev - 1; i >= 0; i--)
            {
                sClause = "SUBSTRING('" + curNodeCode + "',1," + (iCodeLen - i * 4).ToString() + ")";
                whereClause += (string.IsNullOrEmpty(whereClause)) ? sClause : "," + sClause;                
            }

            if (!string.IsNullOrEmpty(whereClause))
            {
                sbClause.Append("AND NEWS_A2_30 IN (").Append(whereClause).Append(")");
            }
            else
            {
                sbClause.Append("AND 1>1");
            }

            string cmdText = "SELECT NEWS_A2_10 AS COLID FROM  NEWS_A2 WHERE" + sbClause.ToString() +
                             " ORDER BY NEWS_A2_30 ASC";
            using (IDataReader iReader = DataAccessor.ExecuteReader(cmdText))
            {
                while (iReader.Read())
                {
                    sReturnStruct += (string.IsNullOrEmpty(sReturnStruct)) ? iReader["COLID"].ToString() :
                                "," + iReader["COLID"].ToString();
                }
            }


            sbClause.Remove(0, sbClause.ToString().Length);
            sbClause = null;
            msCol = null;

            return sReturnStruct;
        }
        #endregion

        #region  信息中心-信息查询
        /// <summary>
        /// get info search tree   //具有浏览，管理，发布权限的栏目
        /// </summary>
        /// <param name="type">info type</param>
        /// <param name="login">login user</param>
        /// <returns></returns>
        public List<MsColumn> GetSearchTree(string type, LoginUser login)
        {
            string sType = (type != "") ? type : "0";
            string cmdText = "SELECT NEWS_A2_10 AS COLID,NEWS_A2_20 AS COLNAME,NEWS_A2_30 AS LAYCODE," +
                             "NEWS_A2_90 AS BROWSERANGE,NEWS_A2_100 AS BROWSETYPE ,NEWS_A2_50 ,NEWS_A2_60,NEWS_A2_70,NEWS_A2_80 FROM NEWS_A2 WHERE NEWS_A2_120='" + sType + "' AND NEWS_A2_140='0' " +
                             "ORDER BY NEWS_A2_30 ASC,NEWS_A2_130 ASC";

            List<MsColumn> mclist = new List<MsColumn>();
            using (IDataReader read = DataAccessor.ExecuteReader(cmdText))
            {
                while (read.Read())
                {
                    MsColumn mc = new MsColumn();
                    mc.columnId = read["COLID"].ToString();
                    mc.columnName = read["COLNAME"].ToString();
                    mc.columnLevelCode = read["LAYCODE"].ToString();
                    mc.pubPurviewId = read["NEWS_A2_50"].ToString();
                    mc.pubPurviewType = read["NEWS_A2_60"].ToString();
                    mc.managePurview = read["NEWS_A2_70"].ToString();
                    mc.managePurviewType = read["NEWS_A2_80"].ToString();
                    mc.browsePurview = read["BROWSERANGE"].ToString();
                    mc.browsePurviewType = read["BROWSETYPE"].ToString();
                    mclist.Add(mc);
                }
            }

            string curOrganId = login.OrganId;
            string curUserId = login.UserId;
            string curDeptId = login.DeptId;
            string curPosId = login.PosId;
            string curClassId = login.ClassId;
            //浏览权限
            StringLib oString;
            bool bCheck = false;
           // string compareValue = string.Empty;

            IEnumerable<MsColumn> queryCol = mclist.Where(c =>
            {
                oString = new StringLib(c.browsePurview);

                bCheck = false;
                switch (c.browsePurviewType)
                {
                    case "0":
                        bCheck = oString.Contains(curUserId);//compareValue = curUserId;

                        break;
                    case "1":
                        bCheck = oString.Contains(curDeptId);//compareValue = curDeptId;
                        break;
                    case "2":
                        bCheck = oString.Contains(curPosId);//compareValue = curPosId;
                        break;
                    case "3":
                        bCheck = oString.Contains(curClassId);//compareValue = curClassId;
                        break;
                    case "8":
                        bCheck = oString.Contains(curOrganId);
                        break;
                    case "9":
                        bCheck = true;
                        break;
                    default:
                        bCheck = false;
                        break;
                }
                return bCheck;
            });

            List<MsColumn> lstCol = queryCol.ToList();

            //取发布权限
            StringLib oStringPub;
            bool pCheck = false;

          //  string compareValue = string.Empty;

            IEnumerable<MsColumn> pubCol = mclist.Where(c =>
            {
                oStringPub = new StringLib(c.pubPurviewId);

                pCheck = false;
                switch (c.pubPurviewType)
                {
                    case "0":
                        pCheck = oStringPub.Contains(curUserId);//compareValue = curUserId;

                        break;
                    case "1":
                        pCheck = oStringPub.Contains(curDeptId);//compareValue = curDeptId;
                        break;
                    case "2":
                        pCheck = oStringPub.Contains(curPosId);//compareValue = curPosId;
                        break;
                    case "3":
                        pCheck = oStringPub.Contains(curClassId);//compareValue = curClassId;
                        break;
                    case "8":
                        pCheck = oStringPub.Contains(curOrganId);
                        break;
                    case "9":
                        pCheck = true;
                        break;
                    default:
                        pCheck = false;
                        break;
                }
                return pCheck;
            });

            List<MsColumn> lstDeliveryCol = pubCol.ToList();

            lstCol = lstCol.Concat(lstDeliveryCol).ToList();

            //取管理权限
            StringLib oStringManage;
            bool mCheck = false;

            IEnumerable<MsColumn> manageCol = mclist.Where(c =>
            {
                oStringManage = new StringLib(c.managePurview);

                mCheck = false;
                switch (c.managePurviewType)
                {
                    case "0":
                        mCheck = oStringManage.Contains(curUserId);//compareValue = curUserId;
                        break;
                    case "1":
                        mCheck = oStringManage.Contains(curDeptId);//compareValue = curDeptId;
                        break;
                    case "2":
                        mCheck = oStringManage.Contains(curPosId);//compareValue = curPosId;
                        break;
                    case "3":
                        mCheck = oStringManage.Contains(curClassId);//compareValue = curClassId;
                        break;
                    case "8":
                        mCheck = oStringManage.Contains(curOrganId);
                        break;
                    case "9":
                        mCheck = true;
                        break;
                    default:
                        mCheck = false;
                        break;
                }
                return mCheck;
            });

            List<MsColumn> lstAdminCol = manageCol.ToList();
            
            lstCol = lstCol.Concat(lstAdminCol).ToList();
            
            // distinct column
            ColCompare MyCompare = new ColCompare();
            
            List<MsColumn> lstReturn = lstCol.Distinct(MyCompare).ToList();

            return lstReturn;
        }

        /// <summary>
        /// 获取所有子节点的层级编号
        /// </summary>
        /// <param name="columnId"></param>
        /// <returns></returns>
        public string GetChildLevelCode(string columnId)
        {
            string childCodes = string.Empty;
            if (columnId == "")
                return childCodes;
            MsColumn colFobj = this.GetMsColumnObj(columnId);
            string fCode = colFobj.columnLevelCode;
            int fCodeLength = fCode.Length;
           
            string cmdText = "SELECT NEWS_A2_30 FROM NEWS_A2 WHERE NEWS_A2_30 LIKE '"+fCode+"%' AND LEN(NEWS_A2_30) <> '"+fCodeLength+"'";
            using (IDataReader ireader = DataAccessor.ExecuteReader(cmdText))
            {
                while (ireader.Read())
                {
                    childCodes += "'"+ireader["NEWS_A2_30"].ToString() + "',";
                }
            }
            if (!string.IsNullOrEmpty(childCodes))
            {
                childCodes = childCodes.Substring(0,childCodes.Length - 1);
            }
            return childCodes;
        }

        #endregion

        /// <summary>
        /// 根据设置权限查询栏目
        /// </summary>
        /// <param name="whereClause"></param>
        /// <returns></returns>
        public string GetRangeColumn(string whereClause)
        {
            string sColumnIds = string.Empty;
            if (string.IsNullOrEmpty(whereClause)) return sColumnIds;
            string cmdText = " SELECT NEWS_A2_10 FROM NEWS_A2 WHERE NEWS_A2_140='0' ";
            if (whereClause != "")
                cmdText += "AND " + whereClause;
            cmdText += " ORDER BY NEWS_A2_30";

            using(IDataReader ireader = DataAccessor.ExecuteReader(cmdText))
            {
                while(ireader.Read())
                {
                    sColumnIds += (sColumnIds == "") ? ireader["NEWS_A2_10"].ToString() : "," + ireader["NEWS_A2_10"].ToString();
                }
            }
            return sColumnIds;
        }

        /// <summary>
        ///  获取所有子节点id
        /// </summary>
        /// <param name="columnCode"></param>
        /// <returns></returns>
        public string GetAllChildColId(string columnCode)
        {
            string sChildCodeIds = string.Empty;
            if (string.IsNullOrEmpty(columnCode)) return sChildCodeIds;

            string flen = columnCode.Length.ToString();

            string cmdText = "SELECT NEWS_A2_10 FROM NEWS_A2 WHERE NEWS_A2_30 LIKE '" + columnCode + "%' AND LEN(NEWS_A2_30) <> '" + flen + "'";
            using(IDataReader ireader = DataAccessor.ExecuteReader(cmdText))
            {
                while(ireader.Read())
                {
                    sChildCodeIds += (sChildCodeIds == "") ? ireader["NEWS_A2_10"].ToString() : "," + ireader["NEWS_A2_10"].ToString();
                }
            }
            return sChildCodeIds;
        }

        /// <summary>
        /// 获取当前用户具有管理权限的栏目
        /// </summary>
        /// <param name="Type"></param>
        /// <returns></returns>
        public List<MsColumn> GetColManage(string Type)
        {
            string sType = (Type != "") ? Type : "0";
            LoginUser login = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            string cmdText = "SELECT NEWS_A2_10 AS COLID,NEWS_A2_20 AS COLNAME,NEWS_A2_30 AS LAYCODE," +
                             "NEWS_A2_70 MANAGEANGE,NEWS_A2_80 AS MANAGETYPE FROM NEWS_A2 WHERE NEWS_A2_120='" + sType + "' AND NEWS_A2_140='0' " +
                             "ORDER BY NEWS_A2_30 ASC,NEWS_A2_130 ASC";

            List<MsColumn> mclist = new List<MsColumn>();
            using (IDataReader read = DataAccessor.ExecuteReader(cmdText))
            {
                while (read.Read())
                {
                    MsColumn mc = new MsColumn();
                    mc.columnId = read["COLID"].ToString();
                    mc.columnName = read["COLNAME"].ToString();
                    mc.columnLevelCode = read["LAYCODE"].ToString();
                    mc.managePurview = read["MANAGEANGE"].ToString();
                    mc.managePurviewType = read["MANAGETYPE"].ToString();
                    mclist.Add(mc);
                }
            }

            string curOrganId = login.OrganId;
            string curUserId = login.UserId;
            string curDeptId = login.DeptId;
            string curPosId = login.PosId;
            string curClassId = login.ClassId;
            StringLib oString;
            bool bCheck = false;
            string compareValue = string.Empty;

            IEnumerable<MsColumn> queryCol = mclist.Where(c =>
            {
                oString = new StringLib(c.managePurview);
                bCheck = false;
                switch (c.managePurviewType)
                {
                    case "0":
                        bCheck = oString.Contains(curUserId);//compareValue = curUserId;

                        break;
                    case "1":
                        bCheck = oString.Contains(curDeptId);//compareValue = curDeptId;
                        break;
                    case "2":
                        bCheck = oString.Contains(curPosId);//compareValue = curPosId;
                        break;
                    case "3":
                        bCheck = oString.Contains(curClassId);//compareValue = curClassId;
                        break;
                    case "8":
                        bCheck = oString.Contains(curOrganId);
                        break;
                    case "9":
                        bCheck = true;
                        break;
                    default:
                        bCheck = false;
                        break;
                }
                return bCheck;
            });

            List<MsColumn> lstCol = new List<MsColumn>();
            foreach (var eCol in queryCol)
            {
                lstCol.Add(eCol as MsColumn);
            }
            return lstCol;
        }

        /// <summary>
        /// 获取当前用户具有发布权限的栏目
        /// </summary>
        /// <param name="Type"></param>
        /// <returns></returns>
        public List<MsColumn> GetColPub(string Type)
        {
            string sType = (Type != "") ? Type : "0";
            LoginUser login = (LoginUser)HttpContext.Current.Session["LogUserInfo"];
            string cmdText = "SELECT NEWS_A2_10 AS COLID,NEWS_A2_20 AS COLNAME,NEWS_A2_30 AS LAYCODE," +
                             "NEWS_A2_50 PUBANGE,NEWS_A2_60 AS PUBTYPE FROM NEWS_A2 WHERE NEWS_A2_120='" + sType + "' AND NEWS_A2_140='0' " +
                             "ORDER BY NEWS_A2_30 ASC,NEWS_A2_130 ASC";

            List<MsColumn> mclist = new List<MsColumn>();
            using (IDataReader read = DataAccessor.ExecuteReader(cmdText))
            {
                while (read.Read())
                {
                    MsColumn mc = new MsColumn();
                    mc.columnId = read["COLID"].ToString();
                    mc.columnName = read["COLNAME"].ToString();
                    mc.columnLevelCode = read["LAYCODE"].ToString();
                    mc.pubPurviewId = read["PUBANGE"].ToString();
                    mc.pubPurviewType = read["PUBTYPE"].ToString();
                    mclist.Add(mc);
                }
            }

            string curOrganId = login.OrganId;
            string curUserId = login.UserId;
            string curDeptId = login.DeptId;
            string curPosId = login.PosId;
            string curClassId = login.ClassId;
            StringLib oString;
            bool bCheck = false;
            string compareValue = string.Empty;

            IEnumerable<MsColumn> queryCol = mclist.Where(c =>
            {
                oString = new StringLib(c.pubPurviewId);
                bCheck = false;
                switch (c.pubPurviewType)
                {
                    case "0":
                        bCheck = oString.Contains(curUserId);//compareValue = curUserId;

                        break;
                    case "1":
                        bCheck = oString.Contains(curDeptId);//compareValue = curDeptId;
                        break;
                    case "2":
                        bCheck = oString.Contains(curPosId);//compareValue = curPosId;
                        break;
                    case "3":
                        bCheck = oString.Contains(curClassId);//compareValue = curClassId;
                        break;
                    case "8":
                        bCheck = oString.Contains(curOrganId);
                        break;
                    case "9":
                        bCheck = true;
                        break;
                    default:
                        bCheck = false;
                        break;
                }
                return bCheck;
            });

            List<MsColumn> lstCol = new List<MsColumn>();
            foreach (var eCol in queryCol)
            {
                lstCol.Add(eCol as MsColumn);
            }
            return lstCol;
        }

    }

    public class ColCompare : IEqualityComparer<MsColumn>
    {

        #region IEqualityComparer<MsColumn> 成员

        public bool Equals(MsColumn x, MsColumn y)
        {
            return (x.columnId == y.columnId);
        }

        public int GetHashCode(MsColumn obj)
        {
            return obj.ToString().GetHashCode();
        }

        #endregion
    }
}
